<template>
  <el-dialog
    :title="!dataForm.homeid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="新房id" prop="newHouseid">
      <el-input v-model="dataForm.newHouseid" placeholder="新房id"></el-input>
    </el-form-item>
    <el-form-item label="二手房id" prop="oldHouseid">
      <el-input v-model="dataForm.oldHouseid" placeholder="二手房id"></el-input>
    </el-form-item>
    <el-form-item label="租房id" prop="rentHouseid">
      <el-input v-model="dataForm.rentHouseid" placeholder="租房id"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="mapid">
      <el-input v-model="dataForm.mapid" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="" prop="adId">
      <el-input v-model="dataForm.adId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sjId">
      <el-input v-model="dataForm.sjId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="yhId">
      <el-input v-model="dataForm.yhId" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          homeid: 0,
          newHouseid: '',
          oldHouseid: '',
          rentHouseid: '',
          mapid: '',
          adId: '',
          sjId: '',
          yhId: ''
        },
        dataRule: {
          newHouseid: [
            { required: true, message: '新房id不能为空', trigger: 'blur' }
          ],
          oldHouseid: [
            { required: true, message: '二手房id不能为空', trigger: 'blur' }
          ],
          rentHouseid: [
            { required: true, message: '租房id不能为空', trigger: 'blur' }
          ],
          mapid: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          adId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sjId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          yhId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.homeid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.homeid) {
            this.$http({
              url: this.$http.adornUrl(`/house/home/info/${this.dataForm.homeid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.newHouseid = data.home.newHouseid
                this.dataForm.oldHouseid = data.home.oldHouseid
                this.dataForm.rentHouseid = data.home.rentHouseid
                this.dataForm.mapid = data.home.mapid
                this.dataForm.adId = data.home.adId
                this.dataForm.sjId = data.home.sjId
                this.dataForm.yhId = data.home.yhId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/home/${!this.dataForm.homeid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'homeid': this.dataForm.homeid || undefined,
                'newHouseid': this.dataForm.newHouseid,
                'oldHouseid': this.dataForm.oldHouseid,
                'rentHouseid': this.dataForm.rentHouseid,
                'mapid': this.dataForm.mapid,
                'adId': this.dataForm.adId,
                'sjId': this.dataForm.sjId,
                'yhId': this.dataForm.yhId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
